﻿// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "SElementUISlate.h"

class SBox;
class SHorizontalBox;
class SButton;
class SEditableText;

UENUM()
enum class EInputNumberException : uint8
{
	NotNumber,
	MaxThenMaxValue,
	MinTnenMinValue
};


DECLARE_DELEGATE_TwoParams(FOnNumberTextException, const FText&, EInputNumberException);

class ELEMENTUISLATE_API SInputNumber : public SElementUISlate
{
public:
SLATE_BEGIN_ARGS(SInputNumber): _Text(FText::FromString("0.0")), _StepStrictly(false), _IsReadOnly(false) {}
		SLATE_ATTRIBUTE(FSlateFontInfo, Font)
		SLATE_ATTRIBUTE(FText, Text)
		SLATE_ATTRIBUTE(float, NumberStep)
		SLATE_ATTRIBUTE(FString, MinValue)
		SLATE_ATTRIBUTE(FString, MaxValue)
		SLATE_ATTRIBUTE(FOptionalSize, LeftButtonWidth)
		SLATE_ATTRIBUTE(FOptionalSize, RightButtonWidth)
		SLATE_ATTRIBUTE(bool, StepStrictly)
		SLATE_ATTRIBUTE(bool, IsReadOnly)
		SLATE_STYLE_ARGUMENT(FButtonStyle, LeftButtonStyle)
		SLATE_STYLE_ARGUMENT(FButtonStyle, RightButtonStyle)
		SLATE_EVENT(FOnNumberTextException, OnNumberTextException)
		SLATE_EVENT(FOnTextChanged, OnNumberTextChanged)
		SLATE_EVENT(FOnClicked, OnLeftButtonClicked)
		SLATE_EVENT(FOnClicked, OnRightButtonClicked)
	SLATE_END_ARGS()

	/** Constructs this widget with InArgs */
	void Construct(const FArguments& InArgs);

	bool SetNumberText(const TAttribute<FText>& NewText);
	FText GetNumberText();

	bool SetNumberStep(const float NumberStep);

	void SetStepStrictly(bool value) { StepStrictly = value; }
	void SetIsReadOnly(bool value) { IsReadOnly = value; }

	bool SetMinValue(const TAttribute<FString>& MinValue);
	FString GetMinValue();

	bool SetMaxValue(const TAttribute<FString>& MaxValue);
	FString GetMaxValue();

	bool SetLeftButtonWidth(const TAttribute<FOptionalSize>& LeftButtonWidth);
	FOptionalSize GeLeftButtonWidth();

	bool SetRightButtonWidth(const TAttribute<FOptionalSize>& RightButtonWidth);
	FOptionalSize GetRightButtonWidth();

	bool SetLeftButtonStyle(const FButtonStyle* Value);
	bool SetRightButtonStyle(const FButtonStyle* Value);

	void SetOnLeftButtonClicked(FOnClicked InOnClicked);
	void SetOnRightButtonClicked(FOnClicked InOnClicked);

	void SetNumberFont(const TAttribute<FSlateFontInfo>& NewFont);

protected:
	void OnNumberTextChangedEvent(const FText& InText);
	void OnNumberTextCommitted(const FText& CommentText, ETextCommit::Type CommitInfo);

protected:
	TAttribute<FSlateFontInfo> Font;
	TAttribute<FText> Text;
	TAttribute<FString> MinValue;
	TAttribute<FString> MaxValue;
	TAttribute<FOptionalSize> LeftButtonWidth;
	TAttribute<FOptionalSize> RightButtonWidth;
	bool StepStrictly;
	bool IsReadOnly;
	float NumberStep;
	const FButtonStyle* LeftButtonStyle;
	const FButtonStyle* RightButtonStyle;
	FOnNumberTextException OnNumberTextException;
	FOnTextChanged OnNumberTextChanged;
	FOnClicked OnLeftButtonClicked;
	FOnClicked OnRightButtonClicked;

private:
	bool ValideNumber(const FText& Content);
	bool ValideNumber(const FString& Content);
	bool ValideMaxNumber(const FString& Content);
	bool ValideMinNumber(const FString& Content);
	float StringToFloat(FString Content);
	float TextToFloat(FText Content);

private:
	TSharedPtr<SBox> OuterBox;
	TSharedPtr<SHorizontalBox> HorizontalLayout;
	TSharedPtr<SBox> LeftButtonBox;
	TSharedPtr<SButton> LeftButton;
	TSharedPtr<SBox> InputTextBox;
	TSharedPtr<SEditableText> InputText;
	TSharedPtr<SBox> RightButtonBox;
	TSharedPtr<SButton> RightButton;
};
